{extend name="public/base" /} {block name="body"}
<div class="content-tabs">
	<ol class="breadcrumb">
		<li>
			<a href="{:url('index/index')}"><i class="fa fa-home"></i></a>
		</li>
		<li><strong>数据库备份</strong></li>
	</ol>
</div>
<div class="row">
	<div class="ibox float-e-margins">
		<div class="ibox-title">
			<ul class="nav nav-tabs">
				<li class="active">
					<a>数据库备份</a>
				</li>
				<li class="">
					<a href="{:url('database/import')}">数据库还原</a>
				</li>
			</ul>
		</div>
		<div class="ibox-content">
			<div class="page-content">
				<div class="row maintop">
					<div class="col-xs-12 col-sm-8">
						<a id="export" href="javascript:;" style="color:#FFFFFF" autocomplete="off">
							<button class="btn btn-sm btn-primary">
                        <i class="ace-icon fa fa-database bigger-110"></i>
                        备份当前数据
                    </button>
						</a>
						<a id="optimize" href="{:url('optimize')}">
							<button class="btn btn-sm btn-primary">
                        <i class="ace-icon fa fa-check-circle bigger-110"></i>
                        优化数据表
                    </button>
						</a>
						<a id="repair" href="{:url('repair')}">
							<button class="btn btn-sm btn-primary">
                        <i class="ace-icon fa fa-check-square-o bigger-110"></i>
                        修复数据表
                    </button>
						</a>

					</div>

				</div>

				<div class="row">
					<div class="col-xs-12">
						<div>
							<form id="export-form" method="post" class="form-horizontal" action="{:url('export')}">
								<table class="table table-striped table-bordered table-hover" id="dynamic-table">
									<thead>
										<tr class="long-th">
											<th width="4%" class="text-center">
												<label class="pos-rel">
                                        <input class="check-all" id='chkAll' checked="chedked" type="checkbox" value="">
                                        <span class="lbl"></span>
                                    </label></th>
											<th>表名</th>
											<th class="hidden-sm hidden-xs">数据量</th>
											<th class="hidden-sm hidden-xs">数据大小</th>
											<th class="hidden-sm hidden-xs">创建时间</th>
											<th style="border-right:#CCC solid 1px;">操作</th>
										</tr>
									</thead>

									<tbody>

										{foreach name="data_list" item="v"}
										<tr>
											<td height="28" align="center">
												<label class="pos-rel">
                                            <input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$v.name}">
                                            <span class="lbl"></span>
                                        </label>
											</td>
											<td>{$v.name}</td>
											<td class="hidden-sm hidden-xs">【{$v.rows}】 条记录 </td>
											<td class="hidden-sm hidden-xs">{$v.data_length|format_bytes}</td>
											<td class="hidden-sm hidden-xs text-center">{$v.create_time}</td>
											<td class="text-center">
												<div class="hidden-sm hidden-xs action-buttons">
													<a class="green" href="{:url('optimize?tables='.$v['name'])}" id="optimize_{$v['name']}" title="优化表">
														<i class="ace-icon fa fa-check-circle bigger-130"></i> 优化
													</a>&nbsp;
													<a class="info" href="{:url('repair?tables='.$v['name'])}" id="repair_{$v['name']}" title="修复表">
														<i class="ace-icon fa fa-check-square-o bigger-130"></i> 修复
													</a>&nbsp;
													<a class="info backup" href="{:url('exportsql?table='.$v['name'])}" title="备份表">
														<i class="ace-icon fa fa-download bigger-130"></i> 备份
													</a>
												</div>
												<div class="hidden-md hidden-lg">
													<div class="inline position-relative">
														<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                                                    <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                                                </button>
														<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
															<li>
																<a href="{:url('optimize?tables='.$v['name'])}" id="optimize_{$v['name']}" class="tooltip-success" data-rel="tooltip" title="" data-original-title="优化表">
																	<span class="green">
																		<i class="ace-icon fa fa-check-circle bigger-120"></i>
																	</span>
																</a>
															</li>
															<li>
																<a href="{:url('repair?tables='.$v['name'])}" id="repair_{$v['name']}" class="tooltip-success" data-rel="tooltip" title="" data-original-title="修复表">
																	<span class="green">
																		<i class="ace-icon fa fa-check-square-o bigger-120"></i>
																	</span>
																</a>
															</li>

															<li>
																<a href="{:url('exportsql?table='.$v['name'])}" class="tooltip-error" data-rel="tooltip" title="" data-original-title="备份表">
																	<span class="red">
																		<i class="ace-icon fa fa-download bigger-120"></i>
																	</span>
																</a>
															</li>
														</ul>
													</div>
												</div>
											</td>
										</tr>
										{/foreach}
										<tr>
											<td height="50" colspan="7" align="left">&nbsp;</td>
										</tr>
									</tbody>
								</table>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<!-- /.page-content -->
{/block} {block name="script"}
<script>
	/* 数据库备份、优化、修复 */
	(function($) {
		$("a[id^=optimize_]").click(function() {
			$.get(this.href, function(data) {
				if(data.code == 1) {
					layer.alert(data.msg, { icon: 6 });
				} else {
					layer.alert(data.msg, { icon: 5 });
				}
			});
			return false;
		});
		$("a[id^=repair_]").click(function() {
			$.get(this.href, function(data) {
				if(data.code == 1) {
					layer.alert(data.msg, { icon: 6 });
				} else {
					layer.alert(data.msg, { icon: 5 });
				}
			});
			return false;
		});

		var $form = $("#export-form"),
			$export = $("#export"),
			tables
		$optimize = $("#optimize"), $repair = $("#repair");
		$optimize.add($repair).click(function() {
			var that = this;
			$.post(this.href, $form.serialize(), function(data) {
				if(data.code == 1) {
					layer.alert(data.msg, { icon: 6 }, function(index) {
						layer.close(index);
					});
				} else {
					layer.alert(data.msg, { icon: 5 }, function(index) {
						layer.close(index);
					});
				}
				setTimeout(function() {
					$('#top-alert').find('button').click();
					$(that).removeClass('disabled').prop('disabled', false);
				}, 1500);
			}, "json");
			return false;
		});

		$export.click(function() {
			$export.children().addClass("disabled");
			$export.children().text("正在发送备份请求...");
			var that = this;
			$.post(
				$form.attr("action"),
				$form.serialize(),
				function(data) {
					if(data.code == 1) {
						tables = data.tables;
						$export.children().text(data.msg + "开始备份，请不要关闭本页面！");
						backup(data.tab);
						window.onbeforeunload = function() {
							return "正在备份数据库，请不要关闭！"
						}
					} else {
						layer.alert(data.msg, { icon: 5 });
						$export.children().removeClass("disabled");
						$export.children().text("立即备份");
						setTimeout(function() {
							$('#top-alert').find('button').click();
							$(that).removeClass('disabled').prop('disabled', false);
						}, 1500);
					}
				},
				"json"
			);
			return false;
		});

		function backup(tab, status) {
			status && showmsg(tab.id, "开始备份...(0%)");
			var that = this;
			$.get($form.attr("action"), tab, function(data) {
				if(data.code == 1) {
					showmsg(tab.id, data.msg);
					if(!$.isPlainObject(data.tab)) {
						$export.children().removeClass("disabled");
						$export.children().text("备份完成，点击重新备份");
						window.onbeforeunload = null;
					}
					if(data.tab != undefined) {
						backup(data.tab, tab.id != data.tab.id);
					}
				} else {
					updateAlert(data.msg, 'alert-error');
					$export.children().removeClass("disabled");
					$export.children().text("立即备份");
					setTimeout(function() {
						$('#top-alert').find('button').click();
						$(that).removeClass('disabled').prop('disabled', false);
					}, 1500);
				}
			}, "json");
		}

		function showmsg(id, msg) {
			$tr = $form.find("input[value=" + tables[id] + "]").closest("tr");
			$tr.find(".green").html("");
			$tr.find(".info").html("");
			$tr.find(".backup").html(msg);
		}
	})(jQuery);
</script>
{/block}